﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/11/11 23:17:15
 * ************************************************************/
namespace Mozlite.Web.UI.Areas.UCenter.Controllers
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Web.Mvc;
    using System.Collections.Generic;
    using Mozlite.FileStorages;
    using Mozlite.Web.UI.Areas.UCenter.Models;
    using Mozlite.Common;

    /// <summary>
    /// 默认控制器。
    /// </summary>
    public class HomeController : UCenterBaseController
    {
        public ActionResult Index()
        {
            return View(Self);
        }

        [HttpPost]
        public ActionResult Index(User model)
        {
            if (ModelState.IsValid)
            {
                model.UserID = Self.UserID;
                model.UserName = Self.UserName;
                model.Email = Self.Email;
                var status = Users.SaveProfile(model);
                SetStatus(GLocal(status), status != CreateUserStatus.Updated);
            }
            return View(Self);
        }

        private UserFolder GetFolder() { 
            int folderID = Request.QueryString["id"].ToInt32();
            UserFolder folder;
            if(!Storages.GetFolders().TryGetValue(folderID, out folder))
                folder = new UserFolder();
            return folder;
        }

        public ActionResult DiskView()
        {
            ViewBag.FileType = this.Get<FileType>("type");
            return View(GetFolder());
        }

        [HttpPost]
        public ActionResult DiskView(FormCollection form)
        {
            var folder = GetFolder();
            ViewBag.FileType = this.Get<FileType>("type");
            if (ModelState.IsValid)
            {
                switch (form.GetAction())
                {
                    case HttpAction.File:
                        {
                            int did = form.Get<int>("did");
                            return DeleteFile(did);
                        }
                    case HttpAction.Folder:
                        {
                            int did = form.Get<int>("did");
                            return DeleteFolder(did);
                        }
                    default:
                        return UploadFile(folder);
                }
            }
            return View(folder);
        }

        public ActionResult ChangePasswordView()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ChangePasswordView(ChangePasswordModel model)
        {
            if (ModelState.IsValid)
            {
                if (!Users.CheckPassword(model.SrcPassword, Self))
                {
                    SetResourceStatus("User.SrcPassword.Error", true);
                    return View(model);
                }
                Users.ChangePassword(Self.UserID, model.NewPassword);
                SetResourceStatus("User.SrcPassword.Success");
            }
            return View(model);
        }

        public ActionResult QuestionAnswerView()
        {
            return View();
        }

        [HttpPost]
        public ActionResult QuestionAnswerView(QuestionAnswerModel model)
        {
            if (ModelState.IsValid)
            {
                if (!Users.CheckPassword(model.Password, Self))
                {
                    SetResourceStatus("User.Password.Error", true);
                    return View(model);
                }
                Users.ChangeSecurityQuestionAndAnswer(Self.UserID, model.PasswordQuestion, model.PasswordAnswer);
                SetResourceStatus("User.QuestionAnswer.Success");
            }
            return View(model);
        }
    }
}